package tyk.Dao;

import java.sql.*;

/***
 *@title JDBU
 *@description <TODO description class purpose>
 *@author Tang'ya'kang
 *@VERSION 1.0.0
 *@CREATE 2023/5/4 4:33
 **/
public class JDBC {
    public static boolean Register(String username,String password,String email) throws ClassNotFoundException, SQLException {
        Class.forName("com.mysql.cj.jdbc.Driver");
        Connection connection= DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/javaweb","root","123456");
        String sql="insert into user (username,password,email) values(?,?,?);";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setObject(1,username);
        preparedStatement.setObject(2,password);
        preparedStatement.setObject(3,email);
        int rows=preparedStatement.executeUpdate();
        preparedStatement.close();
        connection.close();
        if(rows>0) return true;
        else return false;
    }
    public static boolean Exit(String username) throws ClassNotFoundException, SQLException {
        Class.forName("com.mysql.cj.jdbc.Driver");
        Connection connection= DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/javaweb","root","123456");
        String sql="select * from user where username = ?;";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setObject(1,username);
        ResultSet resultSet =preparedStatement.executeQuery();
        if(resultSet.next()) return false;
        else return true;
    }

}
